<template>
  <div class='container'>
<el-form :inline="true" :model="form" class="demo-form-inline">
    <div class="users_main_head">
        <el-form-item  label="学科名称">
          <el-input
            class="users_main_head_search"
            v-model="form.subjectName"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm">清空</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
          <el-button
            type="success"
            class="newElement"
            @click="openDrawer('add')"
            >新增学科</el-button
          >
        </el-form-item>
      </div>
      <div class="uesrs_main_statistics">共{{total}}条记录</div>
      <el-table ref="table" :data="randerList" style="width: 100%">
        <el-table-column prop="id" label="序号" width="50"> </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="150">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="150">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="150">
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="130">
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="140">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="130">
        </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="140">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row }">
            <a @click="disciplineClassification(row,'classify')">学科分类</a>
            <a @click="disciplineClassification(row,'lable')">学科标签</a>
            <a  @click="openDrawer('edit',row.id)">修改</a>
            <a  @click="delDrawer(row.id)">删除</a>
          </template>
        </el-table-column>
      </el-table>
       <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="total"
      >
      </el-pagination>
      </el-form>
      <SubjectsAdd @newDataes='newDataes' :formBase='formBase'  :dialogFormVisible.sync='dialogFormVisible'></SubjectsAdd>
  </div>
</template>

<script>

import moment from 'moment'
import SubjectsAdd from '@/module-hmmm/components/subjects-add'
import { list, remove } from '@/api/hmmm/subjects.js'
export default {
  components: {
    SubjectsAdd
  },
  data () {
    return {
      dialogFormVisible: false,
      formBase: {
        subjectName: '',
        isFrontDisplay: false
      },
      form: {
        subjectName: ''
      },
      query: {
        page: 1,
        pagesiae: 10
      },
      total: 0,
      randerList: []
    }
  },
  created () {
    this.initData()
  },
  methods: {
    //  获取数据渲染页面
    async initData () {
      const { data } = await list(this.query)
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        item.isFrontDisplay = item.isFrontDisplay ? '是' : '否'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 点击搜索发生什么
    async onSubmit () {
      const { data } = await list({ page: this.query.page, pagesiae: this.query.pagesiae, subjectName: this.form.subjectName })
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        item.isFrontDisplay = item.isFrontDisplay ? '是' : '否'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 点击清空触发事件
    resetForm () {
      this.form.subjectName = ''
      this.initData()
    },
    // 点击添加或修改
    openDrawer (type, id) {
      this.dialogFormVisible = true
      if (type !== 'add') {
        this.randerList.forEach(item => {
          if (item.id === id) {
            this.formBase.subjectName = item.subjectName
            this.formBase.isFrontDisplay = item.isFrontDisplay === '是'
            this.form.id = item.id
          }
        })
      }
    },
    async delDrawer (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    disciplineClassification (row, type) {
      if (type === 'lable') {
        this.$router.push({
          path: 'tags',
          query: {
            id: row.id,
            uname: row.subjectName
          }
        })
      } else {
        this.$router.push({
          path: 'directorys',
          query: {
            id: row.id,
            uname: row.subjectName
          }
        })
      }
    },
    async newDataes (data) {
      this.initData()
      console.log(data)
    },
    // 分页点击
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.initData()
    }
  }
}
</script>

<style scoped lang='less'>
// 表单
.demo-form-inline {
  width: 1300px;
  padding: 0 20px;
  background-color: #fff;
  margin: 20px;
  .users_main_head {
    width: 100%;
    padding: 20px 0;
  }
}
/deep/.users_main_head_search {
  width: 200px !important;
  height: 32px;
}
// 新增按钮
.newElement {
  margin-left: 725px;
}
.uesrs_main_statistics {
  font-size: 13px;
  background-color: #f4f4f5;
  color: #909399;
  padding: 10px 20px;
}
a{
  margin-right: 8px;
  color: #409eff;
}
.el-pagination{
  margin-left: 610px;
  margin-top: 20px;
}
</style>
